<template>
    <view style="height: 100%">
        <!-- pages/myAddress/myAddress.wxml -->
        <view v-if="addressList.length > 0" class="container">
            <view class="addressList" v-for="(item, idx) in addressList" :key="idx">
                <view class="address-detail" :id="idx" @tap="selectAdress">
                    <view class="v-center userinfo">
                        <text class="name">{{ item.Fcontacter }}</text>
                        <text>{{ item.Fmobile }}</text>
                    </view>
                    <view class="address">{{ item.Fprovince }}{{ item.Fcity }}{{ item.Fzone }}{{ item.Faddress }}</view>
                </view>

                <view class="actionBar v-center between">
                    <view :id="item.Fid" @tap="setDetault" class="v-center">
                        <text v-if="item.Fisdefault == true" :class="'icon iconfont icon-duihao1 ' + (item.Fisdefault == true ? 'active' : '')"></text>
                        <text :class="'setBtn ' + (item.Fisdefault == true ? 'active' : '')">设为默认</text>
                    </view>
                    <view class="v-center">
                        <view :id="idx" @tap="modifiy" class="actionBtn">编辑</view>
                        <view @tap="deleteAddr" :id="item.Fid" class="actionBtn">删除</view>
                    </view>
                </view>
            </view>
        </view>
        <view v-else class="blank">
            <view class="algin">
                <image mode="widthFix" @tap="addAddress" class="noImg" src="/static/file/images/noadd.png"></image>
            </view>
            <view @tap="addAddress" class="no_msg algin">暂无收货地址！去添加~~</view>
        </view>
        <view :style="'background:' + color" @tap="addAddress" class="bottomBar algin">新增收货地址</view>
    </view>
</template>

<script>
import floatIcon from '@/component/floatIcon/floatIcon';
// pages/myAddress/myAddress.js
var util = require('../../utils/util.js');
var api = require('../../utils/prom.js'); 
export default {
    components: {
        floatIcon
    },
    data() {
        return {
            color: app.globalData.color,
            addressList: [],
            ids: '',
            back: '',
            hide: true,
            nofind: false
        };
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this;
        console.log(options);
        that.back = options.back == undefined ? '' : options.back;
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        this.onShowClone3389();
    },
    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {
        this.onShowClone3389({});
        uni.stopPullDownRefresh();
    },
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        return {
            title: app.globalData.shareinfo.title,
            imageUrl: app.globalData.shareinfo.img,
            desc: app.globalData.shareinfo.desc,
            path: 'pages/index/index?Fid=' + uni.getStorageSync('uid')
            //点击分享的图片进到哪一个页面
        };
    },
    methods: {
        /**
         * 生命周期函数--监听页面显示
         */
        onShowClone3389: function () {
            var that = this;
            that.searchAddress();
        },

        //地址列表
        searchAddress: function () {
            var that = this;
            var param = {
                token: uni.getStorageSync('token')
            };
            api.requestAll('SpAddress', 'get', param, function (res) {
                that.addressList = res.data;
            });
        },

        //选择收货地址
        selectAdress: function (e) {
            var that = this;
            var i = e.currentTarget.id;
            if (that.back == 1) {
                var pages = getCurrentPages();
                var prevPage = pages[pages.length - 2]; //上一个页面
                console.log(prevPage);
                prevPage.doSetAddress(that.addressList[i]);
                //返回上一页
                uni.navigateBack({
                    delta: 1
                });
            }
        },

        //添加地址跳转
        addAddress: function () {
            uni.navigateTo({
                url: '/pages/addAddress/addAddress'
            });
        },

        //删除地址
        deleteAddr: function (e) {
            var that = this;
            console.log(e.currentTarget.id);
            var addrId = e.currentTarget.id;
            uni.showModal({
                title: '提示',
                content: '确定要删除该收货地址吗？',
                success: function (res) {
                    if (res.confirm) {
                        that.delAddress(addrId);
                    }
                }
            });
        },

        delAddress: function (id) {
            var that = this;
            var param = {
                Fid: id
            };
            api.request('SpAddress?token=' + uni.getStorageSync('token') + '&action=del', 'post', param, function (res) {
                that.searchAddress();
                uni.removeStorageSync('addressInfo');
            });
        },

        //编辑跳转
        modifiy: function (e) {
            var that = this;
            var i = e.currentTarget.id;
            uni.navigateTo({
                url: '/pages/addAddress/addAddress?detail=' + escape(JSON.stringify(that.addressList[i]))
            });
        },

        //设为默认
        setDetault: function (e) {
            var that = this;
            console.log(e.currentTarget.id);
            var param = {
                Fid: e.currentTarget.id
            };
            api.requestAll('SpAddress?token=' + uni.getStorageSync('token') + '&action=default', 'post', param, function (res) {
                that.searchAddress();
                uni.removeStorageSync('addressInfo');
            });
        }
    }
};
</script>
<style>
@import './myAddress.css';
</style>
